import datetime

from django.utils             import timezone
from django.test              import TestCase
from django.core.urlresolvers import reverse

from print_request.models     import Request

def create_print_request(name, extension, short, description, days):
  """
  Creates a print_request with the given `question` published the given number of
  `days` offset to now (negative for print_request published in the past,
  positive for print_request that have yet to be published).
  """
  return Request.objects.create(
      name=name,
      extension=extension,
      short_description=short,
      description=description,
      pub_date=timezone.now() + datetime.timedelta(days=days))

class RequestViewTests(TestCase):
  def test_index_view_with_no_print_request(self):
    """
    If no print_request exist, an appropriate message should be displayed.
    """
    response = self.client.get(reverse('print_request:index'))
    self.assertEqual(response.status_code, 200)
    self.assertContains(response, "No print request are available.")
    self.assertQuerysetEqual(response.context['latest_requests'], [])

  def test_index_view_with_a_past_print_request(self):
    """
    Requests with a pub_date in the past should be displayed on the index page.
    """
    create_print_request(
        short="Past print request.",
        name="PAST",
        description="Past one.",
        extension="ply",
        days=-30)
    response = self.client.get(reverse('print_request:index'))
    self.assertQuerysetEqual(
        response.context['latest_requests'],
        ['<Request: PAST: Past one.>']
        )

  def test_index_view_with_a_future_print_request(self):
    """
    Requests with a pub_date in the future should be displayed on the index page.
    """
    create_print_request(
        short="Future print request.",
        name="FUTURE",
        description="Future one.",
        extension="ply",
        days=30)
    response = self.client.get(reverse('print_request:index'))
    self.assertQuerysetEqual(
        response.context['latest_requests'],
        ['<Request: FUTURE: Future one.>']
        )

# vim:tabstop=2:softtabstop=2:shiftwidth=2
